import * as GUI from 'babylonjs-gui'
import Base from "../Base";
import type { AbstractMesh, IMouseEvent, Nullable } from "babylonjs";
import type { Rectangle } from "babylonjs-gui";
import { useAgvApi } from "../../api/agv";
import { useEscapeApi } from "../../api/escape";


export default class Gui {
    private base: Base
    private navPanel: Rectangle | null

    constructor() {
        this.base = Base.getInstance()
        this.createCameraToggleButton()
        this.navPanel = null
        this.createEscapeButton()
    }

    private createCameraToggleButton() {
        // GUI
        const advancedTexture = GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
        const button = GUI.Button.CreateSimpleButton("toggle", "toggle");
        button.width = 0.05;
        button.height = "20px";
        button.color = "white";
        button.fontSize = "15px"
        button.background = "green";
        // 调整按钮的位置
        button.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
        button.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP

        button.onPointerUpObservable.add(() => {
            this.base.toggleCamera()
        });

        advancedTexture.addControl(button);

    }


    public createNavigateMenu(station: Nullable<AbstractMesh> | undefined, event: IMouseEvent) {
        if (this.navPanel !== null) return

        // GUI
        const advancedTexture = GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");

        const navPanel = new GUI.Rectangle();
        navPanel.name = station?.name
        navPanel.width = "100px";
        navPanel.height = "40px";
        navPanel.cornerRadius = 5;
        navPanel.color = "Green";
        navPanel.thickness = 1;
        navPanel.background = "rgba(255,255,255,0.8)";
        navPanel.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        navPanel.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        navPanel.top = event.clientY - 21 + "px"
        navPanel.left = event.clientX + 17 + "px"
        advancedTexture.addControl(navPanel);

        const navButton = GUI.Button.CreateSimpleButton("nav", "导航到" + station?.name);
        navButton.width = 0.06;
        navButton.height = "20px";
        navButton.color = "white";
        navButton.fontSize = "15px"
        navButton.background = "skyblue";
        navButton.cornerRadius = 5;

        navButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        navButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        navButton.top = event.clientY - 20 + "px"
        navButton.left = event.clientX + 20 + "px"




        const cancelButton = GUI.Button.CreateSimpleButton("cancel", "取消");
        cancelButton.width = 0.06;
        cancelButton.height = "20px";
        cancelButton.color = "white";
        cancelButton.fontSize = "15px"
        cancelButton.background = "skyblue";
        cancelButton.cornerRadius = 5;
        // 调整按钮的位置
        cancelButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        cancelButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        cancelButton.top = event.clientY + "px"
        cancelButton.left = event.clientX + 20 + "px"




        navPanel.addControl(navButton)
        navPanel.addControl(cancelButton)
        advancedTexture.addControl(navButton);
        advancedTexture.addControl(cancelButton);
        this.navPanel = navPanel

        // 取消按钮的事件
        cancelButton.onPointerUpObservable.add(() => {
            navPanel.dispose()
            this.navPanel = null
        });
        //  导航按钮的事件
        navButton.onPointerUpObservable.add(async () => {
            // 导航成功
            console.log("开始导航");
            // 发送导航请求给后端
            await useAgvApi().runNav({
                m_type: 3051,
                m_data: {
                    id: station?.name,
                },
            });

            navPanel.dispose()
            this.navPanel = null
        });

    }

    public createEscapeButton() {
        // GUI
        const advancedTexture = GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");


        const controlButton = GUI.Button.CreateSimpleButton("control", "抢占控制权");
        controlButton.width = 0.06;
        controlButton.height = "20px";
        controlButton.color = "white";
        controlButton.fontSize = "15px"
        controlButton.background = "skyblue";
        controlButton.cornerRadius = 5;
        controlButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        controlButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        controlButton.top = "2px"
        controlButton.left = "5px"



        const clearButton = GUI.Button.CreateSimpleButton("clear", "清理任务链");
        clearButton.width = 0.06;
        clearButton.height = "20px";
        clearButton.color = "white";
        clearButton.fontSize = "15px"
        clearButton.background = "skyblue";
        clearButton.cornerRadius = 5;
        clearButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        clearButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        clearButton.top = "2px"
        clearButton.left = "110px"

        const yachaButton = GUI.Button.CreateSimpleButton("tornado", "压差龙卷风");
        yachaButton.width = 0.06;
        yachaButton.height = "20px";
        yachaButton.color = "white";
        yachaButton.fontSize = "15px"
        yachaButton.background = "skyblue";
        yachaButton.cornerRadius = 5;
        yachaButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        yachaButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        yachaButton.top = "2px"
        yachaButton.left = "210px"


        const escapeButton = GUI.Button.CreateSimpleButton("escape", "模拟逃逸");
        escapeButton.width = 0.06;
        escapeButton.height = "20px";
        escapeButton.color = "white";
        escapeButton.fontSize = "15px"
        escapeButton.background = "skyblue";
        escapeButton.cornerRadius = 5;
        escapeButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        escapeButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        escapeButton.top = "2px"
        escapeButton.left = "310px"

        const playButton = GUI.Button.CreateSimpleButton("play", "播放");
        playButton.width = 0.06;
        playButton.height = "20px";
        playButton.color = "white";
        playButton.fontSize = "15px"
        playButton.background = "skyblue";
        playButton.cornerRadius = 5;
        playButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        playButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        playButton.top = "2px"
        playButton.left = "410px"

        const stopPlayButton = GUI.Button.CreateSimpleButton("stop", "停止");
        stopPlayButton.width = 0.06;
        stopPlayButton.height = "20px";
        stopPlayButton.color = "white";
        stopPlayButton.fontSize = "15px"
        stopPlayButton.background = "skyblue";
        stopPlayButton.cornerRadius = 5;
        stopPlayButton.horizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        stopPlayButton.verticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP
        stopPlayButton.top = "2px"
        stopPlayButton.left = "510px"


        advancedTexture.addControl(controlButton);
        advancedTexture.addControl(clearButton);
        advancedTexture.addControl(yachaButton);
        advancedTexture.addControl(escapeButton);
        advancedTexture.addControl(playButton);
        advancedTexture.addControl(stopPlayButton);

        controlButton.onPointerUpObservable.add(async () => {
            await useAgvApi().runConfig({
                m_type: 4005,
                m_data: {
                    nick_name: 'escape',
                },
            });

        });

        clearButton.onPointerUpObservable.add(async () => {
            await useEscapeApi().sendEscape({
                escape: false,
                yacha: false,
                clear: true
            })
        });

        yachaButton.onPointerUpObservable.add(async () => {
            await useEscapeApi().sendEscape({
                escape: false,
                yacha: true,
                clear: false
            })
        });

        escapeButton.onPointerUpObservable.add(async () => {
            await useEscapeApi().sendEscape({
                escape: true,
                yacha: false,
                clear: false
            })
        });

        playButton.onPointerUpObservable.add(async () => {

            await useAgvApi().runOhter({
                m_type: 6000,
                m_data: { name: "navigation", loop: true }
            })
        });

        stopPlayButton.onPointerUpObservable.add(async () => {

            await useAgvApi().runOhter({
                m_type: 6012,
            })

        });

    }
}
